<template>
  <div class="wrapper">
    <div style="margin: 100px auto;background-color: #FFFFFF;width: 350px;height: 400px;padding: 20px;border-radius: 10px;">
      <div style="margin: 20px 0;text-align: center;font-size: 24px;"><b>注 册</b></div>
      <el-form :model="userForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" size="medium" v-model="userForm.username" prefix-icon="el-icon-user" style="margin: 10px 0"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" size="medium" v-model="userForm.password" prefix-icon="el-icon-lock" show-password style="margin: 10px 0"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input placeholder="请确认密码" size="medium" v-model="userForm.confirmPassword" prefix-icon="el-icon-lock" show-password style="margin: 10px 0"></el-input>
        </el-form-item>
        <div style="margin: 10px 0;text-align: right">
          <el-form-item>
            <el-button type="primary" size="small" @click="submitForm('ruleForm')">注册</el-button>
            <el-button type="warning" size="small" @click="$router.push('/login')">返回登录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      userForm: {
        username: '',
        password: '',
        confirmPassword: ""
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.userForm.password !== this.userForm.confirmPassword) {
            this.$message.error("两次密码输入不一致")
          }
          //发送登录请求
          this.request.post("/user/register",this.userForm).then( res => {
            if (res.code === "200") {
              this.$message.success("注册成功")
              this.$refs['ruleForm'].resetFields();
            }else {
              this.$message.error(res.msg)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right,#FC4668,#3F5EF8);
  overflow: hidden;
}
</style>